﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2010
*/
#region Using
using System;
using System.Collections.Generic;
using Rio.Framework.Historical;
using System.Data;
using NetBay.Core.BusinessEntities.Workflow;
using NetBay.Core.Log;

#endregion

namespace Rio.Flow.Business.Repository.Translator
{
    public partial class WorkflowDefinitionTranslator
    {
        #region Public functions
        /// <summary>
        /// Translate a list of workflow models
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static List<WorkflowDefinition> TranslateWorkflowDefinitions(IDataReader DbDataReader)
        {
            var definitions = new List<WorkflowDefinition>();

            try
            {
                using (DbDataReader)
                {
                    while (DbDataReader.Read())
                    {
                        WorkflowDefinition definition = TranslateDefinition(DbDataReader);
                        definitions.Add(definition);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return definitions;
        }

        /// <summary>
        /// Translate one workflow model
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static WorkflowDefinition TranslateWorkflowDefinition(IDataReader DbDataReader)
        {
            var definition = new WorkflowDefinition();

            try
            {
                using (DbDataReader)
                {
                    if (DbDataReader.Read())
                        definition = TranslateDefinition(DbDataReader);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return definition;
        }

        /// <summary>
        /// Translate workflow model actions
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static List<WorkflowAction> TranslateActionsOrConditions(IDataReader DbDataReader)
        {
            var actions = new List<WorkflowAction>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowAction action = new WorkflowAction();
                        action.Identifier = Convert.ToInt32(DbDataReader["r_wkf_def_act_cond_id"].ToString());
                        action.ActionOrConditionId = Convert.ToInt32(DbDataReader["t_ref_wkf_act_cond_id"].ToString());
                        action.Name = DbDataReader["t_ref_wkf_act_cond_name"].ToString();
                        action.Description = DbDataReader["t_ref_wkf_act_cond_desc"].ToString();
                        action.ClassName = DbDataReader["t_ref_wkf_class_name"].ToString();
                        action.AssemblyName = DbDataReader["t_ref_wkf_assembly"].ToString();
                        action.IsTrigger = Convert.ToBoolean(int.Parse(DbDataReader["r_wkf_def_act_cond_is_trigger"].ToString()));
                        actions.Add(action);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return actions;
        }

        /// <summary>
        /// Translate workflow model actions
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        public static List<WorkflowCondition> TranslateConditions(IDataReader DbDataReader)
        {
            var actions = new List<WorkflowCondition>();

            try
            {
                using (DbDataReader)
                {
                    // Read all rows
                    while (DbDataReader.Read())
                    {
                        WorkflowCondition action = new WorkflowCondition();
                        action.Identifier = Convert.ToInt32(DbDataReader["r_wkf_def_act_cond_id"].ToString());
                        action.ActionOrConditionId = Convert.ToInt32(DbDataReader["t_ref_wkf_act_cond_id"].ToString());
                        action.Name = DbDataReader["t_ref_wkf_act_cond_name"].ToString();
                        action.Description = DbDataReader["t_ref_wkf_act_cond_desc"].ToString();
                        action.ClassName = DbDataReader["t_ref_wkf_class_name"].ToString();
                        action.AssemblyName = DbDataReader["t_ref_wkf_assembly"].ToString();
                        action.IsTrigger = Convert.ToBoolean(int.Parse(DbDataReader["r_wkf_def_act_cond_is_trigger"].ToString()));
                        actions.Add(action);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return actions;
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Translate one definition
        /// </summary>
        /// <param name="DbDataReader">Opened datareader</param>
        /// <returns></returns>
        private static WorkflowDefinition TranslateDefinition(IDataReader DbDataReader)
        {
            var definition = new WorkflowDefinition();

            try
            {
                definition.Identifier = Convert.ToInt64(DbDataReader["r_wkf_def_id"].ToString());
                definition.FolderTypeId = Convert.ToInt64(DbDataReader["rp_td_id"].ToString());
                definition.ModelId = Convert.ToInt64(DbDataReader["r_wkf_mdl_id"].ToString());
                
                definition.WorkflowType = (WorkflowType)Enum.Parse(typeof(WorkflowType), DbDataReader["t_ref_wkf_type_id"].ToString());
                definition.ReceiversType = (ReceiverType)Enum.Parse(typeof(ReceiverType), DbDataReader["t_ref_wkf_rec_mode_id"].ToString());

                definition.Name = DbDataReader["r_wkf_def_name"].ToString();
                definition.Description = DbDataReader["r_wkf_def_desc"].ToString();
                definition.ClassName = DbDataReader["r_wkf_def_classname"].ToString();
                definition.AssemblyName = DbDataReader["r_wkf_def_assembly"].ToString();
                definition.Rules = DbDataReader["r_wkf_def_rules"].ToString();
                definition.IsHierarchical = Convert.ToBoolean(int.Parse(DbDataReader["r_wkf_def_Hierarchical"].ToString()));  

                if (DbDataReader["r_wkf_def_id_parent"] != DBNull.Value && !string.IsNullOrEmpty(DbDataReader["r_wkf_def_id_parent"].ToString()))
                {
                    definition.ParentId = Convert.ToInt64(DbDataReader["r_wkf_def_id_parent"].ToString());
                }

                if (DbDataReader["rp_ch_no_receiver"] != DBNull.Value && !string.IsNullOrEmpty(DbDataReader["rp_ch_no_receiver"].ToString()))
                {
                    definition.ReceiverIndexId = Convert.ToInt64(DbDataReader["rp_ch_no_receiver"].ToString());
                }

                definition.StartDate = FieldRow.Translate<DateTime>(DbDataReader["r_wkf_def_start_date"].ToString());

                if (DbDataReader["r_wkf_def_end_date"] != DBNull.Value)
                {
                    definition.EndDate = FieldRow.Translate<DateTime>(DbDataReader["r_wkf_def_end_date"].ToString());
                }
                else
                {
                    definition.EndDate = null;
                }

                definition.HasChild = Convert.ToBoolean(int.Parse(DbDataReader["HasChild"].ToString()));  
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return definition;
        }
        #endregion
    }
}